/**
 * Copyright: Copyright (c)2015
 * Company: niuwap2p
 */
package com.niuwa.p2p.action.borrowinfomg.enterpriseinfomg;

import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.niuwa.p2p.core.mvc.entity.JsonResult;
import com.niuwa.p2p.core.utils.AppConstants;
import com.niuwa.p2p.core.utils.DictConstant;
import com.niuwa.p2p.entity.channel.Corporation;
import com.niuwa.p2p.service.borrowinfomg.enterpriseinfomg.IEnterpriseBasicInfoMgService;
import com.niuwa.p2p.service.borrowinfomg.enterpriseinfomg.IEnterpriseinfoMgService;
import com.niuwa.p2p.vo.borrowinfomg.enterpriseinfomg.CorpBasicInfoVo;

/**
 * @author Jinchao
 * @date 2015年4月21日
 * @version 1.0
 */
public class EnterpriseInfoAction extends EnterpriseCommonAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8254596610070722212L;

	@Autowired
	private IEnterpriseBasicInfoMgService enterpriseBasicInfoMgService;
	@Autowired
	private IEnterpriseinfoMgService enterpriseinfoMgService;

	@Action("addBorrowCorpBasicInfo")
	public String addBorrowCorpBasicInfo() throws Exception {
		JsonResult result = new JsonResult();
		String corpCode;
		if (AppConstants.OPER_ADD.equals(getCoprOperateFlg())) {
			Corporation corp = checkCorporaction();
			if (corp != null) {
				result.setSuccess(false);
				result.setMsgType(DETAIL);
				result.setMsg("该借款人已存在，不能保存！是否查看" + corp.getCorpName() + "(借款人）信息");
				result.setData(corp);
				this.setResult(result);
				return NONE;
			}
		}

		// 设置账号类型
		if (StringUtils.isEmpty(this.getCorpBasicInfoVo().getCorporation().getCorpCode())) {
			this.getCorpBasicInfoVo().getCorporation()
					.setCorpAcctType(DictConstant.CORP_ACCOUNT_TYPE.ENTERPRISE.getCode());
			this.getCorpBasicInfoVo().getCorporation().setCorpType(DictConstant.CORP_TYPE.COMMON_CORP.getCode());
			corpCode = enterpriseBasicInfoMgService.addNewCorpBasicInfo(getCorpBasicInfoVo());
			result.setSuccess(true);
			result.setMsg("企业创建成功");
			result.setData(corpCode);
			this.setResult(result);
		} else {
			enterpriseBasicInfoMgService.updateEnterpirseInfo(this.getCorpBasicInfoVo());
			result.setSuccess(true);
			result.setMsg("企业更新成功");
			this.setResult(result);
		}
		return NONE;
	}

	public Corporation checkCorporaction() {
		Corporation corp = enterpriseBasicInfoMgService.getCorpByName(this.getCorpBasicInfoVo().getCorporation()
				.getCorpName());
		return corp;
	}
}
